﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Maticsoft.Common.DEncrypt;
using Maticsoft.Common;

namespace Maticsoft.Web.wap
{
    public partial class Register : BasePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            AjaxProcess();
        }

        protected void AjaxProcess( )
        {
            if (!string.IsNullOrEmpty(Request.QueryString["sync"]) && Request.QueryString["sync"] == "true")
            {
                //try
                //{
                    BLL.SendMsg msgBLL = new BLL.SendMsg();
                    List<Model.SendMsg> list = msgBLL.GetListByMobile(Request.QueryString["username"]);
                    if (list.Count > 0)
                    {
                        Model.SendMsg msgModel = list[0];
                        if (msgModel.VerifyCode != Request.QueryString["code"])
                        {
                            Response.Write("-1");
                            Response.End();
                        }

                        if (((DateTime)msgModel.CreateTime).AddMinutes(10) < DateTime.Now)
                        {
                            Response.Write("-2");
                            Response.End();
                        }

                        msgModel.Status = 2;
                        msgBLL.Update(msgModel);

                        BLL.Members meBLL = new BLL.Members();
                        Model.Members meModel = new Model.Members();
                        meModel = meBLL.GetModel(Request.QueryString["username"]);
                        if (meModel != null)
                        {
                            Response.Write("-3");
                            Response.End();
                        }
                        meModel = new Model.Members();

                        meModel.UserName = Request.QueryString["username"];
                        meModel.Password = DEncrypt.Encrypt(Request.QueryString["pwd"]);
                        meModel.Mobile = Request.QueryString["username"];
                        meModel.CreateTime = DateTime.Now;
                        meModel.Credits = 0;
                        meModel.GroupId = 3;
                        meModel.IsActive = 1;
                        meModel.LevelId = 1;
                        meModel.Status = 1;
                        meModel.UserId = meBLL.Add(meModel);

                        Session["UserId"] = meModel.UserId;
                        Session["ManagerName"] = meModel.UserName;
                        Session["GroupId"] = meModel.GroupId;
                        Session["LevelId"] = meModel.LevelId;

                        Response.Write("1");
                        Response.End();
                    }
                //}
                //catch (Exception ex)
                //{
                //    Logger.Log4Net.Error(ex.Message + ";" + ex.StackTrace);
                //}
            }
        }
    }
}